#include "coneWindow.h"
#include <QString>
#include <qmetaobject.h>
#include <qobject.h>
#include <qpushbutton.h>
#include <ui_coneWindow.h>
#include "BRepPrimAPI_MakeCone.hxx"

coneWindow::coneWindow(modelSoft* myModelSoft,ConeFeature* m_coneFeature,QWidget *parent)
	: myModelSoft(myModelSoft)
	, coneFeature(m_coneFeature)
	,QWidget(parent)
	, ui(new Ui::coneWindowClass())
{
	ui->setupUi(this);
	ui->radius1Input->setText(QString::number(coneFeature->getRadius1()));
	ui->radius2Input->setText(QString::number(coneFeature->getRadius2()));
	ui->highInput->setText(QString::number(coneFeature->getHigh()));
	shapeID = coneFeature->getShapeId();
	beforeFeature->setShapeId(coneFeature->getShapeId());
	beforeFeature->setLocation(coneFeature->getLocation());
	beforeFeature->setRadius1(coneFeature->getRadius1());
	beforeFeature->setHigh(coneFeature->getHigh());
	beforeFeature->setRadius2(coneFeature->getRadius2());
	//查找该Pipeline是否已存在，存在则获取，不存在则创建
	auto iter1 = myModelSoft->m_pipelineMap.find(coneFeature->getShapeId());
	//Handle(QIVtkSelectionPipeline) conePipeline;
	if (iter1 == myModelSoft->m_pipelineMap.end()) {
		if (myModelSoft->m_historyManager->getHistorySelect()) {
			//另建一个临时Pipeline显示基本体素
			feature->setShapeId(100);
			feature->setLocation(coneFeature->getLocation());
			feature->setRadius1(coneFeature->getRadius1());
			feature->setHigh(coneFeature->getHigh());
			feature->setRadius2(coneFeature->getRadius2());
			coneTemPipeline = myModelSoft->makeCone(feature);
			coneTemPipeline->Actor()->SetPickable(false);
			coneTemPipeline->Actor()->GetProperty()->DeepCopy(myModelSoft->myView->getInteractorStyle()->highlightProperty);
		}
		else {
	conePipeline = myModelSoft->makeCone(coneFeature);
	conePipeline->Actor()->SetPickable(false);
		}
		
	}
	else {
		conePipeline = iter1->second;
		//已存在pipeline,高亮显示
		conePipeline->Actor()->GetProperty()->DeepCopy(myModelSoft->myView->getInteractorStyle()->highlightProperty);
		
	}
	myModelSoft->myView->getRenderWindow()->Render();
	createConnections();
}

void coneWindow::createConnections() {
	//connect(ui->cancelButton, SIGNAL(&QPushButton::clicked), this, SLOT(close()));
	//上面这种连接方式为何不行???
	connect(ui->confirmButton, &QPushButton::clicked, this, &coneWindow::makeCone_onClick);
	connect(ui->cancelButton, &QPushButton::clicked, this, &QWidget::close);
	connect(ui->vertexSelectButton, &QPushButton::clicked, this, &coneWindow::onClickVertexSelect);
	connect(ui->radius1Input, &QLineEdit::textEdited, this, &coneWindow::onRadius1Changed);
	connect(ui->radius2Input, &QLineEdit::textEdited, this, &coneWindow::onRadius2Changed);
	connect(ui->highInput, &QLineEdit::textEdited, this, &coneWindow::onHighChanged);
	connect(myModelSoft->currVertexSelector, &OccVertexSelector::sentVertex, this, &coneWindow::receivePos);
}

double coneWindow::get_cone_radius1() {
	QString lengthText = ui->radius1Input->text();
	return lengthText.toDouble();
}

double coneWindow::get_cone_radius2() {
	QString widthText = ui->radius2Input->text();

	return widthText.toDouble();
}

double coneWindow::get_cone_high() {
	QString highText = ui->highInput->text();
	return highText.toDouble();
}

void coneWindow::receivePos(const TopoDS_Shape& shape)
{
	const TopoDS_Vertex& vertex = TopoDS::Vertex(shape);
	// 获取顶点坐标
	if (!vertex.IsNull()) {
		point_pos = BRep_Tool::Pnt(vertex);
		coneFeature->setLocation(point_pos);
		conePipeline = myModelSoft->updateConePipelineShape(conePipeline, coneFeature);
		conePipeline->Actor()->SetPickable(false);
	}
	else {
		qDebug() << "null vertex";
	}
}

void coneWindow::makeCone_onClick() {
	confirmCreate = true;
	if (myModelSoft->m_historyManager->getHistorySelect()) {
		if (!coneTemPipeline.IsNull()) {
			myModelSoft->RemovePipelineFromRenderer(coneTemPipeline);
		}
		else {
			conePipeline->Actor()->GetProperty()->DeepCopy(myModelSoft->myView->getInteractorStyle()->baseProperty);
		}
	}
	else {
	conePipeline->Actor()->SetPickable(true);
	QStandardItem* item1 = new QStandardItem(QString("圆锥-%1").arg(conePipeline->GetShapeID()));
	int row = myModelSoft->ui->model->rowCount();
	item1->setEditable(false);
	myModelSoft->ui->model->setItem(row, 0, item1);
	QStandardItem* item2 = new QStandardItem(QString("隐藏"));
	item2->setCheckable(true);
	myModelSoft->ui->model->setItem(row, 1, item2);
	}

	close();
}

void coneWindow::closeEvent(QCloseEvent* event)
{
	if (!myModelSoft->m_historyManager->getHistorySelect()) {
		if (!confirmCreate) myModelSoft->RemovePipelineFromRenderer(conePipeline);
	}
	else {
	if (!confirmCreate) {
			if (!coneTemPipeline.IsNull()) {
				myModelSoft->RemovePipelineFromRenderer(coneTemPipeline);
				coneFeature->setHigh(beforeFeature->getHigh());
				coneFeature->setRadius1(beforeFeature->getRadius1());
				coneFeature->setRadius2(beforeFeature->getRadius2());
				std::list<BaseFeature*> list = coneFeature->GetOperationList();
				BaseFeature* lastFeature = list.back();
				myModelSoft->m_historyManager->undoByFeature(lastFeature);
				myModelSoft->m_historyManager->redoByFeature(lastFeature);
			myModelSoft->myView->getRenderWindow()->Render();

				int row1 = myModelSoft->m_historyManager->historyTree->rowCount() - 1;
				myModelSoft->m_historyManager->historyTree->removeRow(row1);
			}
			else {
				conePipeline->Actor()->GetProperty()->DeepCopy(myModelSoft->myView->getInteractorStyle()->baseProperty);

			}
		}
		myModelSoft->m_historyManager->setHistorySelect(false);
	}

	myModelSoft->myView->getRenderWindow()->Render();
}

void coneWindow::onClickVertexSelect()
{
	myModelSoft->myView->getInteractorStyle()->setSelectionMode(SM_Vertex);
	myModelSoft->changeSelector(SM_Vertex);
	myModelSoft->myView->getInteractorStyle()->setCurrSelector(myModelSoft->currSelector);

}

void coneWindow::onRadius1Changed()
{
	coneFeature->setRadius1(ui->radius1Input->text().toDouble());
	std::list<BaseFeature*> list = coneFeature->GetOperationList();
	//从历史树选择，需同步修改已操作步骤
	if (myModelSoft->m_historyManager->getHistorySelect() && list.size() != 0) {
		//撤消重做时已完成递归操作了，只需对最后操作进行撤消重做即可(前面操作会递归的在撤消重做中进行)
		//获取list链尾元素
		feature->setRadius1(ui->radius1Input->text().toDouble());
		coneTemPipeline = myModelSoft->updateConePipelineShape(coneTemPipeline, feature);
		coneTemPipeline->Actor()->GetProperty()->DeepCopy(myModelSoft->myView->getInteractorStyle()->highlightProperty);
		BaseFeature* lastFeature = list.back();
		myModelSoft->m_historyManager->undoByFeature(lastFeature);
		myModelSoft->m_historyManager->redoByFeature(lastFeature);
		myModelSoft->myView->getRenderWindow()->Render();

		int row1 = myModelSoft->m_historyManager->historyTree->rowCount() - 1;
		myModelSoft->m_historyManager->historyTree->removeRow(row1);
	}
	else {
	conePipeline = myModelSoft->updateConePipelineShape(conePipeline, coneFeature);
	conePipeline->Actor()->SetPickable(false);
	}
}

void coneWindow::onRadius2Changed()
{
	coneFeature->setRadius2(ui->radius2Input->text().toDouble());
	std::list<BaseFeature*> list = coneFeature->GetOperationList();
	//从历史树选择，需同步修改已操作步骤
	if (myModelSoft->m_historyManager->getHistorySelect() && list.size() != 0) {
		//撤消重做时已完成递归操作了，只需对最后操作进行撤消重做即可(前面操作会递归的在撤消重做中进行)
		//获取list链尾元素
		feature->setRadius2(ui->radius2Input->text().toDouble());
		coneTemPipeline = myModelSoft->updateConePipelineShape(coneTemPipeline, feature);
		coneTemPipeline->Actor()->GetProperty()->DeepCopy(myModelSoft->myView->getInteractorStyle()->highlightProperty);
		BaseFeature* lastFeature = list.back();
		myModelSoft->m_historyManager->undoByFeature(lastFeature);
		myModelSoft->m_historyManager->redoByFeature(lastFeature);
		myModelSoft->myView->getRenderWindow()->Render();

		int row1 = myModelSoft->m_historyManager->historyTree->rowCount() - 1;
		myModelSoft->m_historyManager->historyTree->removeRow(row1);
	}
	else {
	conePipeline = myModelSoft->updateConePipelineShape(conePipeline, coneFeature);
	conePipeline->Actor()->SetPickable(false);
	}

}

void coneWindow::onHighChanged()
{
	coneFeature->setHigh(ui->highInput->text().toDouble());
	std::list<BaseFeature*> list = coneFeature->GetOperationList();
	//从历史树选择，需同步修改已操作步骤
	if (myModelSoft->m_historyManager->getHistorySelect() && list.size() != 0) {
		//撤消重做时已完成递归操作了，只需对最后操作进行撤消重做即可(前面操作会递归的在撤消重做中进行)
		//获取list链尾元素
		feature->setHigh(ui->highInput->text().toDouble());
		coneTemPipeline = myModelSoft->updateConePipelineShape(coneTemPipeline, feature);
		coneTemPipeline->Actor()->GetProperty()->DeepCopy(myModelSoft->myView->getInteractorStyle()->highlightProperty);
		BaseFeature* lastFeature = list.back();
		myModelSoft->m_historyManager->undoByFeature(lastFeature);
		myModelSoft->m_historyManager->redoByFeature(lastFeature);
		myModelSoft->myView->getRenderWindow()->Render();

		int row1 = myModelSoft->m_historyManager->historyTree->rowCount() - 1;
		myModelSoft->m_historyManager->historyTree->removeRow(row1);
	}
	else {
	conePipeline = myModelSoft->updateConePipelineShape(conePipeline, coneFeature);
	conePipeline->Actor()->SetPickable(false);
	}

}


coneWindow::~coneWindow()
{}
